package com.ybu;

import java.util.Comparator;
import java.util.TreeSet;

public class TreeSetDemo {
    public static void main(String[] args) {
        //集合只能存储引用数据类型

        test1();
    }

    private static void test0() {
        TreeSet<Integer> Set1 = new TreeSet<>();
        //包装成Integer对象 自动装箱
        Set1.add(1);
        Set1.add(2);
        Set1.add(3);
        Set1.add(4);
        Set1.add(5);
        //增强for循环取数据
        for (Integer s : Set1) {
            System.out.println(s);
        }
        //System.out.println(Set1);
    }

    public static void test1() {
        TreeSet<Student> set = new TreeSet<>();
        set.add(new Student("zhangsan", 20));
        set.add(new Student("lisi", 21));
        set.add(new Student("wangwu", 19));
        set.add(new Student("zhaoliu", 23));
        for (Student s : set){
            System.out.println(s);
        }
    }
    public static void test2() {
        /*Comparator<Student>comparator = new Comparator<Student>() {
         @Override
         public int compare(Student o1, Student o2) {
             return o1.getAge() - o2.getAge();
         }
        };*/
        //lambda表达式
        Comparator<Student>comparator = (o1,o2)->o1.getAge() - o2.getAge();
        TreeSet<Student> set = new TreeSet<>(comparator);
        set.add(new Student("zhangsan", 20));
        set.add(new Student("lisi", 21));
        set.add(new Student("wangwu", 19));
        set.add(new Student("zhaoliu", 23));
        for (Student s : set){
            System.out.println(s);
        }
    }
    public static int sum(int... a) {
        int total = 0;
        for (int i = 0; i < a.length; i++) {
            total  += a[i];
        }
        return total;
    }
}
